package com.ruijie.acl.web;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;

import com.ruijie.acl.AclDevice;
import com.ruijie.acl.service.AclDeviceService;
import com.ruijie.batch.plan.BatchPlan;
import com.ruijie.batch.plan.PlanDefinitionRequest;
import com.ruijie.batch.plan.PlanDefinitionService;
import com.ruijie.emp.device.Device;
import com.ruijie.emp.system.DeviceModel;
import com.ruijie.emp.system.DeviceType;
import com.ruijie.emp.system.DeviceVendor;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.service.Repository;
import com.ruijie.foundation.utils.StringUtils;
import com.ruijie.foundation.web.intercept.AddSuccessInfo;

/**
 * <p>
 * Title: ACL设备管理Action类

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 为ACL设备管理模块提供页面数据的显示、获取和页面的跳转。

 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("aclDeviceAction")
@Scope(ScopeType.CONVERSATION)
@AddSuccessInfo
public class AclDeviceAction extends AclBaseAction {
	
	@In("#{aclDeviceService}")
	private AclDeviceService aclDeviceService;
	
	private AclDevice criteria = new AclDevice();
	private Device deviceCriteria = new Device();
	
	private AclDevice model;
	// 从ACL设备管理之外的页面传递进来的ID
    @RequestParameter
    private Long deviceId;
	  
	public static final String MAPPING_TASK = "acl_task_code_acldevice_sync_info";
	public static final String MAPPING_PLAN = "System-AclSync";
	@In("#{planDefinitionService}")
	private PlanDefinitionService planDefinitionService;
	private PlanDefinitionRequest planRequest;

	//设备类型
	private DeviceType deviceType = new DeviceType();
	//设备型号
	private String deviceModelName;
	private DeviceVendor vendor;
	//是否已部署ACL:是
	private Boolean chkYes = false;
	//是否已部署ACL:否
	private Boolean chkNo = false;
	//同步时间
	private String time;
	
	
	/**
	 * 取得设备分页信息
	 * @return
	 */
	protected Page pagedQuery(EntityObject criteria, int page, int pageSize) {
		AclDevice entity = (AclDevice)criteria;
		//设置查询设备的型号和类型
		DeviceModel deviceModel = new DeviceModel();
		if(deviceType != null &&  deviceType.getCode()!= null) {
			deviceModel.setDeviceType(deviceType);
		}
		
		if(!StringUtils.isNullOrEmpty(deviceModelName)) {
			deviceModel.setName(deviceModelName);
		}
		deviceCriteria.setDeviceModel(deviceModel);
		entity.setDevice(deviceCriteria);
		//设置是否已部署ACL：当选择是：已部署；当选择否：未部署
		//其余情况，全部查询
		if(chkYes) {
			if(!chkNo) {
				entity.setAclflag(true);
			}else {
				entity.setAclflag(null);
			}
		}else {
			if(!chkNo) {
				entity.setAclflag(null);
			}else {
				entity.setAclflag(false);
			}
		}
		return aclDeviceService.pageQuery(entity,vendor, page, pageSize);
		
	}
	
	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return aclDeviceService;
	}

	@Override
	protected String getAddPage() {
		return "/acl/aclDeviceImport.seam";
	}

	@Override
	public EntityObject getCriteria() {
		return criteria;
	}

	@Override
	protected String getDetailPage() {
		return "/acl/aclDeviceDetail.seam";
	}

	@Override
	protected String getListPage() {
		return "/acl/aclDeviceIndex.seam";
	}

	@Override
	public EntityObject getModel() {
		if (this.model == null && this.deviceId != null){
			this.model = aclDeviceService.getByDeviceId(this.deviceId); 
		}
		return model;
	}

	@Override
	protected String getUpdatePage() {
		return "/acl/aclDeviceEdit.seam";
	}

	@Override
	public void resetModel() {
		model = null;
		model = new AclDevice();

	}

	@Override
	public void setCriteria(EntityObject criteria) {
		this.criteria = (AclDevice)criteria;

	}

	@Override
	public void setModel(EntityObject model) {
		this.model = (AclDevice)model;
	}

	public Device getDeviceCriteria() {
		return deviceCriteria;
	}

	public void setDeviceCriteria(Device deviceCriteria) {
		this.deviceCriteria = deviceCriteria;
	}
	/**
	 * 取得自动执行时间
	 * @return 自动执行时间
	 */
	public String getTime() {

		if (this.time == null) {
			try {
				BatchPlan batchPlan = planDefinitionService.getBatchPlanByName(
						MAPPING_PLAN, MAPPING_TASK);
				this.planRequest = new PlanDefinitionRequest(batchPlan);

				this.time = planRequest.getFrequency().getTargetHour() + ":"
						+ planRequest.getFrequency().getTargetMinute();
			} catch (Exception ex) {
				this.time = "0:00";
			}
		}
		String strHour = this.time.split(":")[0];
		String strMinute = "0" + this.time.split(":")[1];
		strMinute = strMinute.substring(strMinute.length() - 2, strMinute.length());
		this.time = strHour + ":" + strMinute;
		return this.time;
	}

	public void setTime(String time) {
		this.time = time;
	}
	
	/**
	 * 保存频率
	 */
	public void SaveFrequency() {
		String strHour = this.getTime().split(":")[0];
		String strMinute = this.getTime().split(":")[1];
		this.planRequest.getFrequency()
				.setTargetHour(Integer.parseInt(strHour));
		this.planRequest.getFrequency().setTargetMinute(
				Integer.parseInt(strMinute));
		planDefinitionService.saveOrUpdateBatchPlan(this.planRequest);
		addGlobalI18nMessage("command_update_success");
	}

	public DeviceType getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(DeviceType deviceType) {
		this.deviceType = deviceType;
	}

	public String getDeviceModelName() {
		return deviceModelName;
	}

	public void setDeviceModelName(String deviceModelName) {
		this.deviceModelName = deviceModelName;
	}

	public void setChkYes(Boolean chkYes) {
		this.chkYes = chkYes;
	}
	
	public Boolean getChkYes() {
		return this.chkYes;
	}
	
	public Boolean getChkNo() {
		return chkNo;
	}

	public void setChkNo(Boolean chkNo) {
		this.chkNo = chkNo;
	}
	public Long getDeviceId() {
		return deviceId;
    }

    public void setDeviceId(Long deviceId) {
        this.deviceId = deviceId;
    }
    
	public DeviceVendor getVendor() {
		return vendor;
	}

	public void setVendor(DeviceVendor vendor) {
		this.vendor = vendor;
	}
	/**
	 * 同步ACL设备
	 */
	public void syncAclDevice(){
		this.aclDeviceService.syncAclDevice(this.model.getDevice());
		this.setModel(this.aclDeviceService.get(this.model.getId()));
		addGlobalI18nMessage("emp_device_mapping_sync_success");
		this.toDetailPage(this.model);
	}
	/**
	 * 根据设备名称，进入设备详细页面
	 * @param deviceName
	 */
	public String toDetailPage(String deviceName,String previousPage){
		this.model=aclDeviceService.getByName(deviceName);
		if(this.model==null){
			return null;
		}
		return this.toDetailPage(this.model,previousPage);
	}
	/**
	 * 根据设备进入ACL设备详细页面
	 * @param device
	 * @return
	 */
	public String toDetailPageByDevice(Device device,String previousPage){
		this.model=this.aclDeviceService.getByDevice(device);
		if(this.model!=null){
			return this.toDetailPage(model, previousPage);
		}else{
			return null;
		}
	}
	
	//-------------modify by sinyee 2010-10-22 being----------------

	public String toUpdatePageBlank(EntityObject model){
		this.toUpdatePage(model);
		return null;
	}
	
	@AddSuccessInfo
	public String updateEntity(){
		this.update();
		return null;
	}
	
	//--------------modify by sinyee 2010-10-22 end----------------
	
}
